package com.jzfai.service;

import com.jzfai.entity.Role;
import com.jzfai.mapper.RoleMapper;
import com.jzfai.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.ListUtils;

import java.util.HashMap;
import java.util.List;

/**
 * @Title: RoleService
 * @Description:
 * @Auther:kuanghua
 * @Version: 1.0
 * @create 2019/8/16 15:42
 */
@Service
public class RoleService {
    @Autowired
    RoleMapper roleMapper;

    public JsonData addRole(Role role){
        Role role1 = new Role();
        role1.setTitle(role.getTitle());
        List<Role> roles = roleMapper.selectListRole(role1);
        if(!ListUtils.isEmpty(roles)){
            return JsonData.buildError("角色已存在");
        }
        int i = roleMapper.insertRole(role);
        if(i>0){
            return  JsonData.buildSuccess();
        }else{
            return JsonData.buildError("添加角色失败");
        }
    }

    public JsonData editRole(Role role){
        int i = roleMapper.updateRole(role);
        if(i>0){
            return  JsonData.buildSuccess();
        }else{
            return JsonData.buildError("更新角色失败");
        }
    }

    public JsonData deleteRole(Integer id){
        int i = roleMapper.deleteByIdRole(id);
        if(i>0){
            return  JsonData.buildSuccess();
        }else{
            return JsonData.buildError("删除角色失败");
        }
    }

    public JsonData getListRole(Role role){
        List<Role> listRole = roleMapper.selectListRole(role);
        HashMap<String , Object> HashMap = new HashMap<>();
        HashMap.put("pageNum",10);
        HashMap.put("total",100);
        HashMap.put("list",listRole);
        return  JsonData.buildSuccess(HashMap);
    }
}
